package com.xinqi.modules.user.auth.controller.app;

import com.xinqi.common.core.enums.DeviceType;
import com.xinqi.common.log.asynclog.annotation.AsyncLog;
import com.xinqi.common.log.asynclog.enums.AsyncOperaType;
import com.xinqi.common.log.asynclog.enums.AsyncType;
import com.xinqi.modules.user.auth.form.*;
import com.xinqi.modules.user.auth.service.SysAppLoginService;
import com.xinqi.modules.user.user.dto.req.UserRegisterDTO;
import com.xinqi.common.base.response.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * app端登录注册 控制器
 *
 * @version V1.0
 * @Title:
 * @ClassName: com.xinqi.modules.auth.controller.AppTokenController.java
 * @Description:
 * @Copyright 2022-2023 www.xinqi.modules.com - Powered By 星奇科技
 * @author: dgyu
 * @date: 2023-03-09 11:01
 */
@RestController
@RequestMapping("/app")
@RequiredArgsConstructor
public class AppTokenController {

    private final SysAppLoginService sysAppLoginService;

    /**
     * 校验用户是否已注册
     *
     * @param mobile
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @GetMapping("/checkuser/register/{mobile}")
    public R<Void> checkUserRegister(@PathVariable("mobile") String mobile) {
        return sysAppLoginService.checkUserRegister(mobile);
    }

    /**
     * 账号密码登陆
     * @param form
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @AsyncLog(title = "账号密码登陆", asyncType = AsyncType.LOGIN, operatorType = AsyncOperaType.USER_LOGIN)
    @PostMapping("/account/login")
    public R<Map<String, Object>> loginApp(@Validated @RequestBody LoginBody form) {
        return sysAppLoginService.login(form, DeviceType.ANDROID, false);
    }

    /**
     * 账号密码加密登陆
     *
     * @param form
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @AsyncLog(title = "账号密码登陆", asyncType = AsyncType.LOGIN, operatorType = AsyncOperaType.USER_LOGIN)
    @PostMapping("/account/ase/login")
    public R<Map<String, Object>> loginAppAse(@Validated @RequestBody LoginBody form) {
        return sysAppLoginService.login(form, DeviceType.ANDROID, true);
    }

    /**
     * 手机号验证码登陆
     *
     * @param smsLoginBody
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @AsyncLog(title = "手机号验证码登陆", asyncType = AsyncType.LOGIN, operatorType = AsyncOperaType.USER_LOGIN)
    @PostMapping("/mobile/login")
    public R<Map<String, Object>> mobileLogin(@Validated @RequestBody SmsLoginBody smsLoginBody) {
        return sysAppLoginService.smsLogin(smsLoginBody, DeviceType.ANDROID);
    }

    /**
     * 手机号验证码注册接口
     * @param registerDto
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @AsyncLog(title = "手机号验证码注册", asyncType = AsyncType.REGISTER, operatorType = AsyncOperaType.USER_REGISTER)
    @PostMapping("/register")
    public R<Map<String, Object>> register(@RequestBody UserRegisterDTO registerDto) {
        registerDto.setPlatform(DeviceType.ANDROID.getDevice());
        return sysAppLoginService.registerAndSignIn(registerDto);
    }

    /**
     * @author: dgyu
     * @Return
     * @Description: 刷新token
     * @date: 2023-03-06 20:15
     */
    @PostMapping("/flushUserToken")
    public R<Map<String, Object>> flushUserToken(@RequestBody FlushTokenBody body) {
        body.setDeviceType(DeviceType.ANDROID.getDevice());
        return sysAppLoginService.flushUserToken(body);
    }

    /**
     * 忘记密码第一步校验手机号验证码
     *
     * @param check
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @PostMapping("/checkmobile/password")
    public R<Void> forgetOnePwd(@RequestBody SmsCheckBody check) {
        sysAppLoginService.justCheck(check);
        return R.ok("校验成功");
    }

    /**
     * app端忘记密码
     *
     * @param passwordBody
     * @author: dgyu
     * @Return
     * @Description: app端忘记密码
     * @date: 2023-03-06 20:15
     */
    @AsyncLog(title = "用户忘记密码", asyncType = AsyncType.USER_INFO, operatorType = AsyncOperaType.USER_INFO)
    @PostMapping("/appResetpwd")
    public R<Void> resetpwd(@Validated @RequestBody PasswordBody passwordBody) {
        return sysAppLoginService.resetpwd(passwordBody);
    }

    /**
     * 刷新Token过期时间
     * @author: dzy
     * @date: 2023-03-11 16:08
     */
    @GetMapping("/fresh/token")
    public R<Void> freshTokenTime() {
        sysAppLoginService.freshTokenTime();
        return R.ok();
    }

    /**
     * 退出登录
     *
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @ReturnF
     * @Description:
     */
    @AsyncLog(title = "退出登录", asyncType = AsyncType.LOGOUT, operatorType = AsyncOperaType.USER_LOGOUT)
    @PostMapping("/logout")
    public R<Void> logout() {
        sysAppLoginService.logout();
        return R.ok();
    }
}
